package com.gsx.controller;

import com.gsx.bean.Cart;
import com.gsx.bean.User;
import com.gsx.dao.CartMapper;
import com.gsx.service.CartService;
import com.gsx.service.GoodService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description TODO
 * @Authoe gsx
 * @Date 2021/04/08 上午 10:43
 **/
@Controller
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;
    @Resource
    private  GoodService goodService;
    /**
     *用户购物车
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCart")
    public List<Cart> getListCart(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user!=null){
            return cartService.getListByUid(user.getUserId());
        }
        return null;
    }

    /**
     * 获得结算信息
     * @param cartId
     * @return
     */
    @RequestMapping("/closeAnAccount")
    public String  closeAnAccount(Integer[] cartId, Model model){

        List<List<Cart>> listByCid1 = cartService.getListByCid(cartId);
        model.addAttribute("carts",listByCid1);
        return "/html/cart.jsp";
    }

    /**
     * 通过session添加购物车
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addCart")
    public String addCart(HttpServletRequest request){
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart!=null){
        session.setAttribute("cart",null);
        return cartService.setCart(cart)+"";
        }else {
            return false+"";
        }
    }
}
